﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;
using Model;
using System.Threading;

namespace BLL
{
    public class EntityBLL
    {
        EntityDAL dal = new EntityDAL();
        EntityCategoryDAL entityCategoryDAL = new EntityCategoryDAL();
        EntityKeyDAL entityKeyDAL = new EntityKeyDAL();

        public List<EntityView> GetAllEntityView()
        {
            List<EntityView> keyViewList = new List<EntityView>();
            var list1 = dal.GetAll();

            list1.ForEach(x =>
            {
                EntityView view = new EntityView { ID = x.ID, Title = x.Title, Content = x.Content };
                var category = from xx in entityCategoryDAL.GetAllByEID(x.ID) select xx.CID;
                view.CategoryList = string.Join(",", category);
                var keys = from xx in entityKeyDAL.GetAllByEID(x.ID) select xx.KID;
                view.KeyList = string.Join(",", keys);
                keyViewList.Add(view);
            });

            return keyViewList;
        }


        public void Save(List<EntityView> list)
        {
            EntityCategory ec = null;
            EntityKey ek = null;
            Entity entity = null;

            list.ForEach(x =>
            {
                if (x.ID <= 0)//Insert
                {
                    entity = new Entity { Title = x.Title, Content = x.Content };
                    dal.Insert(entity);
                    x.ID = entity.ID;
                }
                else
                {
                    entity = new Entity { ID = x.ID, Title = x.Title, Content = x.Content };
                    dal.Update(entity);
                    entityCategoryDAL.DeleteAllByEID(x.ID);
                    entityKeyDAL.DeleteAllByEID(x.ID);
                }

                if (x.CategoryList != null)
                {
                    var categorys = x.CategoryList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string cid in categorys)
                    {
                        ec = new EntityCategory { EID = x.ID, CID = int.Parse(cid) };
                        entityCategoryDAL.Insert(ec);
                    }
                }

                if (x.KeyList != null)
                {
                    var keys = x.KeyList.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string kid in keys)
                    {
                        ek = new EntityKey { EID = x.ID, KID = int.Parse(kid) };
                        entityKeyDAL.Insert(ek);
                    }
                }
            });
        }

        public void Delete(List<EntityView> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (!list[i].Selected) { continue; }
                if (list[i].ID > 0)
                {
                    entityKeyDAL.DeleteAllByEID(list[i].ID);
                    entityCategoryDAL.DeleteAllByEID(list[i].ID);
                    dal.Delete(list[i]);
                }
            }
        }
    }
}
